// -*- mode:c++ -*-
//
// 2009-2010 HIT Microelectronic center all rights reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met: redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer;
// redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution;
// neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Date: Dec. 2009
// Authors: Gou Pengfei

////////////////////////////////////////////////////////////////////
//
// Base class for TRIPS instructions, and some support functions
//

//Outputs to decoder.hh
output header {{

    using namespace TripsISA;

    /**
     * Base class for all TRIPS static instructions.
     */
    class TRIPSStaticInst : public EDGEStaticInst
    {
      protected:

        // Constructor
        TRIPSStaticInst(const char *mnem, MachInst _machInst, OpClass __opClass)
            : EDGEStaticInst(mnem, _machInst, __opClass)
        {
        }

        /// 
        void printConsumer(std::ostream &os, int id) const;
        std::string printConsumer(int id) const;

        std::string generateDisassembly(Addr pc, const SymbolTable *symtab) const;
    };

}};

//Ouputs to decoder.cc
output decoder {{

    void TRIPSStaticInst::printConsumer(std::ostream &os, int id) const
    {
     	    uint32_t slot = bits(_consumerID[id], 8, 7);
    	    
            ccprintf(os, "t%d", id);
            if ( slot == 0 && bits(_consumerID[id], 6, 5) == 1)
                ccprintf(os, "[w%d ", bits(_consumerID[id], 4, 0 ));
            else
	        ccprintf(os, "[%d,", bits(_consumerID[id], 6, 0 ));
	        if ( slot == 1 ) ccprintf(os, "p] ");
	        else if ( slot == 2 ) ccprintf(os, "op0] ");
	        else if ( slot == 3 ) ccprintf(os, "op1] ");
	        else
	            ccprintf(os, "0] ");
    }

     std::string TRIPSStaticInst::printConsumer( int id) const
    {
    	    std::stringstream ss;
    	    
     	    uint32_t slot = bits(_consumerID[id], 8, 7);
    	    
            ccprintf(ss, "t%d", id);
            if ( slot == 0 && bits(_consumerID[id], 6, 5) == 1)
                ccprintf(ss, "[w%d] ", bits(_consumerID[id], 4, 0 ));
            else
	        ccprintf(ss, "[%d,", bits(_consumerID[id], 6, 0 ));
	        if ( slot == 1 ) ccprintf(ss, "p] ");
	        else if ( slot == 2 ) ccprintf(ss, "op0] ");
	        else if ( slot == 3 ) ccprintf(ss, "op1] ");
	        else
	            ccprintf(ss, "0] ");
	    return ss.str();
    }

    std::string TRIPSStaticInst::generateDisassembly(Addr pc, const SymbolTable *symtab) const
    {
        std::stringstream ss;

        ccprintf(ss, "%-10s ", mnemonic);

        if (strcmp(mnemonic, "syscall") != 0) {

            if(_numConsumers > 0) {
                printConsumer(ss, 0);
            }

            if(_numConsumers > 1) {
                ss << ", ";
                printConsumer(ss, 1);
            }
        }

        return ss.str();
    }

}};

